<template>
  <div>
    <!--列表-->
    <el-table :data="userList" :default-sort="{prop: 'id'}">
      <el-table-column label="id" prop="id" sortable></el-table-column>
      <el-table-column label="用户名" prop="username"></el-table-column>
      <el-table-column label="姓名" prop="name"></el-table-column>
      <el-table-column label="性别" prop="sex">
        <template slot-scope="scope">
          {{ scope.row.sex === 1 ? "男" : scope.row.sex === 2 ? "女" : "未知" }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" prop="phone" sortable></el-table-column>
      <el-table-column label="头像" prop="wxAvatar">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" alt="" style="width: 50px; height: 50px; border-radius: 50%;">
        </template>
      </el-table-column>
      <el-table-column label="账号状态">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.status === 1"
              active-color="green"
              inactive-color="red"
              :disabled="scope.row.status === 3"
              @change="updateUserStatus(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <el-row style="float: right">
      <el-pagination
          @current-change="changePageNo"
          @size-change="changePageSize"
          :current-page.sync="searchParams.pageNo"
          :page-size.sync="searchParams.pageSize"
          :page-sizes="[1, 5, 10, 50, 250]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-row>

  </div>
</template>

<script>
import * as userAdminApi from "../api/userAdminApi";

export default {
  name: "userAdminIndex",
  data() {
    return {
      // 查询参数
      searchParams: {
        pageNo: 1,
        pageSize: 10,
      },
      total: 0,
      userList: [],
    }
  },
  mounted() {
    this.getUserList()
  },
  methods: {
    // 获取数据
    getUserList() {
      userAdminApi.getUserList({
        pageNo: this.searchParams.pageNo,
        pageSize: this.searchParams.pageSize,
        success: result => {
          this.userList = result.data.list;
          this.total = result.data.total;
        }
      })
    },
    // 切换页数
    changePageNo(pageNo) {
      this.searchParams.pageNo = pageNo;
      this.getUserList()
    },
    // 改变页面大小
    changePageSize(pageSize) {
      this.searchParams.pageSize = pageSize;
      this.getUserList()
    },
    // 改变账号状态
    updateUserStatus(user) {
      let newStatus = user.status === 1 ? 2 : 1
      userAdminApi.updateUserStatus({
        userId: user.id,
        newStatus: newStatus,
        success: () => {
          user.status = newStatus
        }
      })
    },
  }
}
</script>
